package cn.ulc.common.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.model.dto.ccb.QrPayUrlDemo;
import cn.ulc.common.model.dto.ccb.QrUrlDemo;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class CCBHelper {
    private static final Log log = LogFactory.get();
    public static final String BANK_URL = "https://ibsbjstar.ccb.com.cn/CCBIS/ccbMain";

    /**
     *
     * @param orderNumber 订单号
     * @param amount  订单收款金额
     * @return
     */
    public static String qrUrlcreate(String orderNumber , String amount) {
        log.info("qrUrlcreate payment:" + amount);
        String bankURL= BANK_URL;
        String MERCHANTID = "105371154110034";
        String POSID = "492382286";
        String BRANCHID = "370000000";
        String ORDERID = orderNumber;
        String PAYMENT= amount;//"0.01";
        String CURCODE="01";
        String TXCODE = "530550";//530550     520100
        String REMARK1 = "";
        String REMARK2 = "";
        String RETURNTYPE="3";
        String TIMEOUT = "";
        String PUB32TR2= "e6c24817f2970064c50fde5f020111";

        StringBuffer tmp = new StringBuffer(); //验签字段
        tmp.append("MERCHANTID=");
        tmp.append(MERCHANTID);
        tmp.append("&POSID=");
        tmp.append(POSID);
        tmp.append("&BRANCHID=");
        tmp.append(BRANCHID);
        tmp.append("&ORDERID=");
        tmp.append(ORDERID);
        tmp.append("&PAYMENT=");
        tmp.append(PAYMENT);
        tmp.append("&CURCODE=");
        tmp.append(CURCODE);
        tmp.append("&TXCODE=");
        tmp.append(TXCODE);
        tmp.append("&REMARK1=");
        tmp.append(REMARK1);
        tmp.append("&REMARK2=");
        tmp.append(REMARK2);
        tmp.append("&RETURNTYPE=");
        tmp.append(RETURNTYPE);
        tmp.append("&TIMEOUT=");
        tmp.append(TIMEOUT);
        tmp.append("&PUB=");
        tmp.append(PUB32TR2);

        LinkedHashMap<String,String> map = new LinkedHashMap<>();
        map.put("CCB_IBSVersion","V6");	//必输项
        map.put("MERCHANTID",MERCHANTID);
        map.put("BRANCHID",BRANCHID);
        map.put("POSID",POSID);
        map.put("ORDERID",ORDERID);
        map.put("PAYMENT",PAYMENT);
        map.put("CURCODE",CURCODE);
        map.put("TXCODE",TXCODE);
        map.put("REMARK1",REMARK1);
        map.put("REMARK2",REMARK2);
        map.put("RETURNTYPE",RETURNTYPE);
        map.put("TIMEOUT",TIMEOUT);
        String mac = SecureUtil.md5("MERCHANTID=" + MERCHANTID + "&POSID=" + POSID + "&BRANCHID=" + BRANCHID + "&ORDERID=" + ORDERID + "&PAYMENT="+amount+"&CURCODE=01&TXCODE=530550&REMARK1=&REMARK2=&RETURNTYPE=3&TIMEOUT=&PUB=" + PUB32TR2);
        map.put("MAC", mac);
        System.out.println("MAC: "+mac);
        String ret = HttpClientUtil.httpPost(bankURL, map); 	//请求二维码生成链接串
        System.out.println("first request ret::"+ret);

        QrPayUrlDemo qrPayUrl = JSONUtil.toBean(ret,QrPayUrlDemo.class);
        System.out.println(qrPayUrl.getPAYURL());
        ret = HttpClientUtil.httpGet(qrPayUrl.getPAYURL()); //获取二维码串
        System.out.println("ret==>::"+ret);
        QrUrlDemo qrUrl =  JSONUtil.toBean(ret, QrUrlDemo.class);
        try{
            return URLDecoder.decode(qrUrl.getQRURL(),"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return qrUrl.getQRURL();
        }
    }

    public static String query(String orderNo,String amount,Date createAt) {
        String MERCHANTID ="105371154110034";
        String BRANCHID="370000000";                 //���д���
        String POSID="492382286";                    //��̨��
        String ORDERDATE= DateUtil.format(createAt , "yyyyMMdd");                  //��������
        String BEGORDERTIME="00:00:00";
        String ENDORDERTIME="23:59:59";
        String BEGORDERID="";
        String ENDORDERID="";
        String QUPWD="111111";
        String TXCODE="410408";
        String SEL_TYPE="3";
        String OPERATOR="";
        //txcode=410408
        String TYPE="0";
        String KIND="0";
        String STATUS="3";
        String ORDERID = orderNo;
        String PAGE = "1";
        String CHANNEL = "";

        String param ="MERCHANTID="+MERCHANTID+"&BRANCHID="+BRANCHID+"&POSID="+POSID+"&ORDERDATE="+ORDERDATE+"&BEGORDERTIME="+BEGORDERTIME
                +"&ENDORDERTIME="+ENDORDERTIME+"&BEGORDERID="+BEGORDERID+"&ENDORDERID="+ENDORDERID+"&QUPWD=&TXCODE="+TXCODE
                +"&SEL_TYPE="+SEL_TYPE+"&OPERATOR="+OPERATOR;

        if("410408".equals(TXCODE)){
            param ="MERCHANTID="+MERCHANTID+"&BRANCHID="+BRANCHID+"&POSID="+POSID+"&ORDERDATE="
                    +ORDERDATE+"&BEGORDERTIME="+BEGORDERTIME+"&ENDORDERTIME="+ENDORDERTIME+"&ORDERID="
                    +ORDERID+"&QUPWD=&TXCODE="+TXCODE+"&TYPE="+TYPE+"&KIND="+KIND+"&STATUS="+STATUS+
                    "&SEL_TYPE="+SEL_TYPE+"&PAGE="+PAGE+"&OPERATOR="+OPERATOR+"&CHANNEL="+CHANNEL;
        }
        System.out.println("-----"+ SecureUtil.md5(param));

        Map map = new HashMap();
        map.put("MERCHANTID",MERCHANTID);
        map.put("BRANCHID",BRANCHID);
        map.put("POSID",POSID);
        map.put("ORDERDATE",ORDERDATE);
        map.put("BEGORDERTIME",BEGORDERTIME);
        map.put("ENDORDERTIME",ENDORDERTIME);
        map.put("BEGORDERID",BEGORDERID);
        map.put("ENDORDERID",ENDORDERID);
        map.put("QUPWD",QUPWD);
        map.put("TXCODE",TXCODE);

        if("410408".equals(TXCODE)){
            map.put("TYPE",TYPE);
            map.put("KIND",KIND);
            map.put("STATUS",STATUS);
            map.put("ORDERID",ORDERID);
            map.put("PAGE",PAGE);
            map.put("CHANNEL",CHANNEL);
        }
        map.put("SEL_TYPE",SEL_TYPE);
        map.put("OPERATOR",OPERATOR);
        map.put("MAC",SecureUtil.md5(param));
        System.out.println(BANK_URL);
        String ret = HttpClientUtil.httpPost(BANK_URL, map);
        System.out.println("ret::"+ret);
        return ret;
    }





}
